<?php
include_once("lib/redisdriver.php");
include_once("lib/logger.php");
include_once("lib/httpclient.php");
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');

function consume(){
	$redis = new Redis();
	$logger = new Logger('debug');
	$config = json_decode(file_get_contents("config/config.json"),true);
	$config = $config['config'];
    $ret = $redis->connect($config['redis_config']['host'],$config['redis_config']['port']);
    if (!$ret) {
		$logger->debug('customer','redis connect error!');
		exit(0);
    }
    $ret = $redis->auth($config['redis_config']['pass']);
    if (!$ret) {
		$logger->debug('customer','redis auth error!');
		exit(0);
	}
	$redis->select(3);
	
	$token = '';
	$http = new HttpClient();
	
	//获取上报token
	if($redis->exists('report_token')){
		 $token = $redis->hget('report_token','token');
	}else{
		$url = $config['report_config']['get_token_url'];	
		$req_data = array('data'=>array('cid'=>$config['report_config']['cid'],'username'=>$config['report_config']['user'],
		    'passwd'=>$config['report_config']['pass']));
		$logger->debug('customer','request token: '.json_encode($req_data));
		$result = $http->AsyncPostjson($url,$req_data);
		$logger->debug('customer','response token: '.$result);
		$result = json_decode($result,true);
		if ($result['code'] == 200){
			$token = $result['info'];
			$redis->hset('report_token','token',$token);
			$redis->expire('report_token',$config['report_config']['reportTokenTime']);
		}else{
			$logger->debug('customer','获取token fail');
			exit(0);
		}
	}
		
	//消费支付队列
	$len = $redis->Llen('pay_data');
	if ($len > 0){
		$value = $redis->lpop('pay_data');
		//上报支付数据
		$url = $config['report_config']['report_pay_url'];
		$valueArr = json_decode($value,true);
		$data = array('data'=>array($valueArr),'cid'=>$config['report_config']['cid'],'secret_key'=>$token);
		$res = $http->AsyncPostjson($url,$data);
		$logger->debug('customer','response pay: '.$res);		
	}
	
	//消费登录队列
	$len = $redis->Llen('login_data');
	if ($len > 0){
	    $value = $redis->lpop('login_data');
	    //上报登录数据
	    $url = $config['report_config']['report_login_url'];
	    $valueArr = json_decode($value,true);
	    $data = array('data'=>array($valueArr),'cid'=>$config['report_config']['cid'],'secret_key'=>$token);
	    $res = $http->AsyncPostjson($url,$data);
	    $logger->debug('customer','response login: '.$res);
	}
	
	//消费注册队列
	$len = $redis->Llen('register_data');
	if ($len > 0){
	    $value = $redis->lpop('register_data');
	    //上报注册数据
	    $url = $config['report_config']['report_register_url'];
	    $valueArr = json_decode($value,true);
	    $data = array('data'=>array($valueArr),'cid'=>$config['report_config']['cid'],'secret_key'=>$token);
	    $res = $http->AsyncPostjson($url,$data);
	    $logger->debug('customer','response register: '.$res);
	}
	
	//消费在线队列
	$len = $redis->Llen('online_data');
	if ($len > 0){
	    $value = $redis->lpop('online_data');
	    //上报在线数据
	    $url = $config['report_config']['report_online_url'];
	    $valueArr = json_decode($value,true);
	    $data = array('data'=>array($valueArr),'cid'=>$config['report_config']['cid'],'secret_key'=>$token);
	    $res = $http->AsyncPostjson($url,$data);
	    $logger->debug('customer','response online: '.$res);
	}
	
	
	$redis->close();
}
consume();

?>